package Application.Color;

import Application.AppJoint;
import javafx.scene.paint.Color;

public class BlendLayer {
    // 无混合
    public static int MODE_NORMAL = 0;
    // 正片叠底
    public static int MODE_MULTIPLY = 1;

    public static BlendLayer getInstance() {
        return ins;
    }

    public static BlendLayer ins = new BlendLayer();
    // c1覆盖在c2之上
    public Color blend(Color c1, Color c2, int mode) {
        Color ans = null;
        if(mode == BlendLayer.MODE_MULTIPLY) {
            double r = (c1.getRed() + c2.getRed()) / 2;
            double g = (c1.getGreen() + c2.getGreen()) / 2;
            double b = (c1.getBlue() + c2.getBlue()) / 2;
            ans = new Color(r, g, b, 1);
        } else if(mode == BlendLayer.MODE_NORMAL) {
            double r = c1.getRed();
            double g = c1.getGreen();
            double b = c1.getBlue();
            ans = new Color(r, g, b, 1);
        }

        return ans;
    }
    // c1 在 c2 之上
    public int blend(int c1, int c2, int mode) {
        int ans = 0;
        //System.out.println(c1);
        int a1 = (c1 >> 24) & 0xff;
        int r1 = (c1 & 0x00ff0000) >> 16;
        //System.out.println(r1);
        int g1 = (c1 & 0x0000ff00) >> 8;
        //System.out.println(g1);
        int b1 = (c1 & 0x000000ff);
        //System.out.println(b1);
        int a2 = (c2 >> 24) & 0xff;
        int r2 = (c2 & 0x00ff0000) >> 16;
        int g2 = (c2 & 0x0000ff00) >> 8;
        int b2 = (c2 & 0x000000ff);
        if(mode == BlendLayer.MODE_MULTIPLY) {
            //int a = (c1 & 0xff000000) + (c2 & 0xff000000) >> 1;
            int a = a1 * a2 / 255;
            int r = r1 * r2 / 255;
            int g = g1 * g2 / 255;
            int b = b1 * b2 / 255;
            ans = (a << 24) | (r << 16) | (g << 8) | b;
        } else if (mode == BlendLayer.MODE_NORMAL) {
            int a = (a1 * a1 + a2 * (255 - a1)) / 255;
            int r = (r1 * a1 + r2 * (255 - a1)) / 255;
            int g = (g1 * a1 + g2 * (255 - a1)) / 255;
            int b = (b1 * a1 + b2 * (255 - a1)) / 255;
            ans = (a << 24) | (r << 16) | (g << 8) | b;
        }

        return ans;
    }

    public static int getBlendModeIntegerByName(String name) {
        switch(name) {
            case "正片叠底":
                return MODE_MULTIPLY;
            case "正常":
                return MODE_NORMAL;
        }

        return -1;
    }
}
